package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.IGoodsService;
import com.twentyOne.ClothingLand.service.impl.GoodsService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.Goods;

public class GoodsAction extends ActionSupport {

	private Goods goods;
	private IGoodsService goodsService = new GoodsService();
	private Double minPrice;
	private Double maxPrice;

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public Double getMinPrice() {
		return minPrice;
	}

	public void setMinPrice(Double minPrice) {
		this.minPrice = minPrice;
	}

	public Double getMaxPrice() {
		return maxPrice;
	}

	public void setMaxPrice(Double maxPrice) {
		this.maxPrice = maxPrice;
	}

	public String addGoods() throws Exception {

		goodsService.addGoods(goods);
		return null;

	}

	public String deleteGoods() throws Exception {

		goodsService.deleteGoods(goods);
		return null;

	}

	public String modifyGoods() throws Exception {

		goodsService.modifyGoods(goods);
		return null;

	}

	public String searchAllGoods() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Goods> list = goodsService.searchAllGoods();

		if (list != null) {
			String goodsJson = JsonUtil.objectsToJson(list);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchGoodsByID() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Goods go = goodsService.searchGoodsByID(goods.getGoodsId());

		if (go != null) {
			String goodsJson = JsonUtil.objectToJson(go);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchGoodsByName() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Goods> list = goodsService.searchGoodsByName(goods.getName());

		if (list != null) {
			String goodsJson = JsonUtil.objectsToJson(list);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchGoodsByClassId() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Goods> list = goodsService.searchGoodsByClassId(goods.getClassId());

		if (list != null) {
			String goodsJson = JsonUtil.objectsToJson(list);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchGoodsByPrice() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Goods> list = goodsService.searchGoodsByPrice(minPrice, maxPrice);

		if (list != null) {
			String goodsJson = JsonUtil.objectsToJson(list);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

	public String searchGoodsByBrandId() throws Exception {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Goods> list = goodsService.searchGoodsByBrandId(goods.getBrandId());

		if (list != null) {
			String goodsJson = JsonUtil.objectsToJson(list);
			out.print(goodsJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

}
